//
//  CommentTableViewCell.m
//  初稿
//
//  Created by lanou3g on 15/7/18.
//  Copyright (c) 2015年 多飞科技. All rights reserved.
//

#import "CommentTableViewCell.h"
#import "MyHandle.h"
#import "ParameterHeader.h"
@implementation CommentTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)setComment:(Comment *)comment{
    
    if (comment != _comment) {
        _comment = comment;
    }
    
////    如果要用观察者，为了保证label2maxY的值一定会变，所以这里给它设置初值
//    self.label2maxY=1.0f;
    
    
    
//    给头像图片变成圆形
    self.comment_avatarImageView.layer.cornerRadius = 20;
    self.comment_avatarImageView.layer.masksToBounds = YES; 
    
//    contentLabel高度自适应
    CGFloat contentLabelHeight = [MyHandle stringHeight:self.comment.content fontSize:12 width:self.contentLabel.frame.size.width];
    
    self.contentLabel.frame = CGRectMake(self.contentLabel.frame.origin.x, self.contentLabel.frame.origin.y, self.contentLabel.frame.size.width, contentLabelHeight);
    
    self.comment_nameLabel.text = self.comment.comment_name;
    self.comment_timeLabel.text = self.comment.comment_timeSB;
    
    self.contentLabel.text = self.comment.content;
    self.praise_countLabel.text = self.comment.praise_countSB;
    self.storeyLabel.text = [self.comment.storeySB stringByAppendingString:@"楼"];
    

    
    
    
    
    
    
    
    //当数据源中有两条以上的reply时，会正常显示label1和label2
    if (self.comment.replyArray.count>=2) {
        self.firstReplyLabel.hidden=NO;
        self.secondReplyLabel.hidden = NO;
        for (int i=0; i<2; i++) {
            
            Reply *reply=(Reply *)self.comment.replyArray[i];
            
            NSString *replyLabelText =[NSString stringWithFormat:@"%@: %@",reply.reply_name,reply.comment];
            
            if (i==0) {
                self.firstReplyLabel.text=replyLabelText;
            }
            else{
                self.secondReplyLabel.text=replyLabelText;
            }
        }
        
        CGFloat firstReplyLabelHeight = [MyHandle stringHeight:self.firstReplyLabel.text fontSize:12 width:self.firstReplyLabel.frame.size.width];
        CGFloat secondReplyLabelHeight = [MyHandle stringHeight:self.secondReplyLabel.text fontSize:12 width:self.secondReplyLabel.frame.size.width];
        //    确定firstReplyLabel的frme
        self.firstReplyLabel.frame = CGRectMake(self.firstReplyLabel.frame.origin.x, self.praiseButton.frame.origin.y+15, self.firstReplyLabel.frame.size.width, firstReplyLabelHeight);
        //    确定secondReplyLabel的frme
        self.secondReplyLabel.frame = CGRectMake(self.secondReplyLabel.frame.origin.x, self.firstReplyLabel.frame.origin.y+15, self.secondReplyLabel.frame.size.width, secondReplyLabelHeight);
    }
    else if(self.comment.replyArray.count==1){
        self.firstReplyLabel.hidden=NO;
        self.secondReplyLabel.hidden = YES;
        
        Reply *reply=(Reply *)self.comment.replyArray[0];
        
        NSString *replyLabelText =[NSString stringWithFormat:@"%@: %@",reply.reply_name,reply.comment];
        
        self.firstReplyLabel.text=replyLabelText;
        
        CGFloat firstReplyLabelHeight = [MyHandle stringHeight:self.firstReplyLabel.text fontSize:12 width:self.firstReplyLabel.frame.size.width];
        self.firstReplyLabel.frame = CGRectMake(self.firstReplyLabel.frame.origin.x, self.praiseButton.frame.origin.y+15, self.firstReplyLabel.frame.size.width, firstReplyLabelHeight);
        
        self.secondReplyLabel.frame=CGRectMake(0, CGRectGetMaxY(self.firstReplyLabel.frame), 10, 0);
        
    }
    else{
        self.firstReplyLabel.frame=CGRectMake(0, CGRectGetMaxY(self.praiseButton.frame), 10, 0);
        self.firstReplyLabel.hidden = YES;
        self.secondReplyLabel.frame=CGRectMake(0, CGRectGetMaxY(self.praiseButton.frame), 10, 0);
        self.secondReplyLabel.hidden = YES;
        
    }
    
    
    self.firstReplyLabel.backgroundColor = [UIColor colorWithWhite:0.906 alpha:1.000];
    self.secondReplyLabel.backgroundColor =  [UIColor colorWithWhite:0.906 alpha:1.000];
    
    
//    CGFloat temp=CGRectGetMaxY(self.secondReplyLabel.frame);
//    self.label2maxY=[NSString stringWithFormat:@"%g",temp];
    
}






- (IBAction)dianzan:(id)sender {
    
    static int i = 0;
    if (i == 0) {
        
        NSString *praise = self.praise_countLabel.text;
        NSInteger a = [praise integerValue];
        a = a+1;
        CAKeyframeAnimation *k = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        k.values = @[@(0.1),@(1.0),@(1.5)];
        k.keyTimes = @[@(0.0),@(0.5),@(0.8),@(1.0)];
        k.calculationMode = kCAAnimationLinear;
        [self.praiseButton.layer addAnimation:k forKey:@"SHOW"];
        NSString *praiseStr = [NSString stringWithFormat:@"%lu",a];
        self.praise_countLabel.text = praiseStr;
        i = 1;
        
    }else{
        
        NSString *praise = self.praise_countLabel.text;
        NSInteger a = [praise integerValue];
        a = a-1;
        CAKeyframeAnimation *k = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        k.values = @[@(0.1),@(1.0),@(1.5)];
        k.keyTimes = @[@(0.0),@(0.5),@(0.8),@(1.0)];
        k.calculationMode = kCAAnimationLinear;
        [self.praiseButton.layer addAnimation:k forKey:@"SHOW"];
        NSString *praiseStr = [NSString stringWithFormat:@"%lu",a];
        self.praise_countLabel.text = praiseStr;
        i = 0;
        
    }
    
    
}

@end
